package com.ruoyi.ucar.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.CheXinMengGetReportBoReq;
import com.ruoyi.ucar.boreq.OrderDetailQueryBoReq;
import com.ruoyi.ucar.boresp.CheXinMengGetReportBoResp;
import com.ruoyi.ucar.boresp.OrderDetailQueryBoResp;
import com.ruoyi.ucar.common.CommonCheXinMengBoResp;
import com.ruoyi.ucar.service.ITOrderService;
import com.ruoyi.ucar.service.UCarQueryApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Duration;

@RestController
@RequestMapping("/ucar/proxy")
@Api(value = "html转发管理",tags = "html转发管理模块接口")
public class ProxyController {

    @Autowired
    private ITOrderService tOrderService;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WebClient webClient;
    @Autowired
    private UCarQueryApiService uCarQueryApiService;
    @ApiOperation("订单的报告查询方式1-mobile使用")
    @GetMapping("/proxyGetReport")
    @Anonymous
    public void proxyGetReport(HttpServletResponse response, @RequestParam String orderUuid) throws IOException {
        logger.info("订单的报告查询-mobile使用入参:{}",orderUuid);
        String html = "       <!DOCTYPE html><html><body style='margin:0'> " +
                "订单编号orderUuid为空！" +
                "           </body></html>";
        if (StringUtils.isEmpty(orderUuid)) {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(html);
        }else{
        OrderDetailQueryBoReq orderDetailQueryBoReq = new OrderDetailQueryBoReq();
        orderDetailQueryBoReq.setOrderUuid(orderUuid);
        OrderDetailQueryBoResp orderDetailQueryBoResp = tOrderService.orderDetailQuery2(orderDetailQueryBoReq);
        if (StringUtils.isNull(orderDetailQueryBoResp)) {
            html = "       <!DOCTYPE html><html><body style='margin:0'> " +
                    "订单编号为orderUuid的订单数据为空！" +
                    "           </body></html>";
        } else {
            boolean flag=false;
            if(StringUtils.isEmpty(orderDetailQueryBoResp.getQueryreportUrl())){
                CheXinMengGetReportBoReq cheXinMengGetReportBoReq=new CheXinMengGetReportBoReq();
                cheXinMengGetReportBoReq.setOrderId(orderDetailQueryBoResp.getQueryreportOrderId());
                CommonCheXinMengBoResp commonCheXinMengBoResp= uCarQueryApiService.cheXinMengGetReport(cheXinMengGetReportBoReq);
                if(StringUtils.isNotNull(commonCheXinMengBoResp.getReturnObj())){
                    orderDetailQueryBoResp.setQueryreportUrl(((CheXinMengGetReportBoResp)commonCheXinMengBoResp.getReturnObj()).getReportUrl());
                    flag=true;
                }
            }

            if (StringUtils.isNotEmpty(orderDetailQueryBoResp.getQueryreportUrl())&&StringUtils.isNotEmpty(orderDetailQueryBoResp.getQueryreportOrderId())) {
                html = webClient.get()
                        .uri(orderDetailQueryBoResp.getQueryreportUrl())
                        .retrieve()
                        .bodyToMono(String.class)
                        .block(Duration.ofSeconds(20));
                if(flag) {
                     html=html.replace("/cxsh/assets/index.5cec6c2a.js","https://bgjk6.chexinmeng.com/cxsh/assets/index.5cec6c2a.js");
                     html=html.replace("/cxsh/assets/index.274886ca.css","https://bgjk6.chexinmeng.com/cxsh/assets/index.274886ca.css");
                }
            }else{
                html = "       <!DOCTYPE html><html><body style='margin:0'> " +
                        "订单编号为orderUuid的订单数据查询报告url为空！" +
                        "           </body></html>";
            }

          }
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(html);
    }


    @ApiOperation("订单的报告查询方式2-mobile使用")
    @GetMapping("/iframeGetReport")
    @Anonymous
    public void iframeGetReport(HttpServletResponse response, @RequestParam String orderUuid) throws IOException {
        logger.info("订单的报告查询-mobile使用入参:{}",orderUuid);
        String html = "       <!DOCTYPE html><html><body style='margin:0'> " +
                "订单编号orderUuid为空！" +
                "           </body></html>";
        if (StringUtils.isEmpty(orderUuid)) {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(html);
        }else{
            OrderDetailQueryBoReq orderDetailQueryBoReq = new OrderDetailQueryBoReq();
            orderDetailQueryBoReq.setOrderUuid(orderUuid);
            OrderDetailQueryBoResp orderDetailQueryBoResp = tOrderService.orderDetailQuery2(orderDetailQueryBoReq);
            if (StringUtils.isNull(orderDetailQueryBoResp)) {
                html = "       <!DOCTYPE html><html><body style='margin:0'> " +
                        "订单编号为orderUuid的订单数据为空！" +
                        "           </body></html>";
            } else {
                if(StringUtils.isEmpty(orderDetailQueryBoResp.getQueryreportUrl())){
                    CheXinMengGetReportBoReq cheXinMengGetReportBoReq=new CheXinMengGetReportBoReq();
                    cheXinMengGetReportBoReq.setOrderId(orderDetailQueryBoResp.getQueryreportOrderId());
                    CommonCheXinMengBoResp commonCheXinMengBoResp= uCarQueryApiService.cheXinMengGetReport(cheXinMengGetReportBoReq);
                    if(StringUtils.isNotNull(commonCheXinMengBoResp.getReturnObj())){
                        orderDetailQueryBoResp.setQueryreportUrl(((CheXinMengGetReportBoResp)commonCheXinMengBoResp.getReturnObj()).getReportUrl());
                    }
                }
                if (StringUtils.isNotEmpty(orderDetailQueryBoResp.getQueryreportUrl())) {
                 html= "<!DOCTYPE html><html><body style='margin:0'>" +
                         "<iframe src=\"" + orderDetailQueryBoResp.getQueryreportUrl() + "\" " +
                         "style=\"width:100%;height:100vh;border:none\"></iframe>" +
                         "</body></html>";
                }else{
                    html = "       <!DOCTYPE html><html><body style='margin:0'> " +
                            "订单编号为orderUuid的订单数据查询报告url为空！" +
                            "           </body></html>";
                }

            }
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(html);
    }

}